package book;

public class MatchSolution {
    // 设计循环赛日程表
    int[][] match(int n) {
        int[][] a = new int[n+1][n+1];
        for (int i=1; i<=n; i++) a[1][i] = i;
        int m=1;
        while (n>1) {
            n/=2;
            for (int t=1; t<=n; t++)
                for (int i=m+1; i<=2*m; i++)
                    for (int j=m+1; j<=2*m; j++) {
                        a[i][j+(t-1)*m*2] = a[i-m][j+(t-1)*m*2-m];
                        a[i][j+(t-1)*m*2-m]=a[i-m][j+(t-1)*m*2];
                    }
            m*=2;
        }
        return a;
    }
    public static void main(String[] args) {
        int[][] table = new MatchSolution().match(8);
        for (int i=1; i<=7; i++)
            System.out.print("\tR" + i);
        System.out.println();
        for (int i=1; i<=8; i++) {
            for (int j=1; j<=8; j++)
                if (j == 1)
                    System.out.print("P" + table[i][1]);
                else
                    System.out.print("\t" + table[i][j]);
            System.out.println();
        }
    }
}
